package com.xh.bussiness.exam.service.impl;

import com.xh.bussiness.exam.bean.dto.ExerciseStudentQuestionDto;
import com.xh.bussiness.exam.bean.request.ExerciseStudentQuestionRequest;
import com.xh.bussiness.exam.model.ExerciseStudentQuestion;
import com.xh.bussiness.exam.dao.ExerciseStudentQuestionMapper;
import com.xh.bussiness.exam.model.StudentExercise;
import com.xh.bussiness.exam.service.IExerciseStudentQuestionService;
import com.xh.bussiness.tiku.bean.dto.PaperDto;
import com.xh.bussiness.tiku.bean.dto.PaperQuestionDto;
import com.xh.bussiness.tiku.model.Paper;
import com.xh.bussiness.tiku.model.Question;
import com.xh.bussiness.tiku.service.IPaperService;
import com.xh.bussiness.tiku.service.IQuestionService;
import com.xh.core.exception.XhException;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
/**
 * <p>
 * 课堂练习学生试题记录表 服务实现类
 * </p>
 *
 * @author Pitt Cheung
 * @since 2021-08-26
 */
@Service
public class ExerciseStudentQuestionServiceImpl extends BaseServiceImpl<ExerciseStudentQuestionMapper, ExerciseStudentQuestion> implements IExerciseStudentQuestionService {

    @Autowired
    private IPaperService paperService;

    @Autowired
    private IQuestionService questionService;
    @Override
    public XhPage queryExerciseStudentQuestionPage(XhPage page, ExerciseStudentQuestionRequest bean) {
        QueryWrapper qw=new QueryWrapper<ExerciseStudentQuestion>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List queryExerciseStudentQuestion(ExerciseStudentQuestionRequest bean) {
        QueryWrapper qw=new QueryWrapper<ExerciseStudentQuestion>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }

    @Override
    public List<ExerciseStudentQuestionDto> queryStudentQuestion(ExerciseStudentQuestion studentQuestion) {
        List<ExerciseStudentQuestionDto> studentQuestionDtos = new ArrayList<>();
//        Paper paper = paperService.getById(studentQuestion.getPaperId());
//        if(paper==null){
//            throw new XhException("为查询到试卷");
//        }
//        if(paper.getTotalScore()==null || paper.getTotalScore()<=0){
//            throw new XhException("试卷总分为0");
//        }
        //查询试题
        List<String> questionIds = paperService.selectPaperQuestion(studentQuestion.getPaperId());
        if(questionIds==null || questionIds.size()==0){
            throw new XhException("该试卷下没有试题");
        }
        studentQuestionDtos = questionService.queryQuestionDetail(questionIds,studentQuestion.getPaperId());
        //查询学生试卷情况
        QueryWrapper<ExerciseStudentQuestion> studentQuestionQW = new QueryWrapper<>();
        studentQuestionQW.eq("student_id",studentQuestion.getStudentId());
        studentQuestionQW.eq("class_id",studentQuestion.getClassId());
        studentQuestionQW.eq("curr_id",studentQuestion.getCurrId());
        studentQuestionQW.eq("paper_id",studentQuestion.getPaperId());
        List<ExerciseStudentQuestion> questions = this.selectList(studentQuestionQW,ExerciseStudentQuestion.class);
        if(questions!=null||questions.size()>0 ){
            for(ExerciseStudentQuestionDto exerciseStudentQuestionDto : studentQuestionDtos){
                for(ExerciseStudentQuestion question : questions){
                    if(exerciseStudentQuestionDto.getId().equals(question.getQuestionId())){
                        exerciseStudentQuestionDto.setGoal(question.getScore());
                        exerciseStudentQuestionDto.setCorrect(question.getCorrect());
                        continue;
                    }
                }
            }
        }
        return studentQuestionDtos;
    }

    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(ExerciseStudentQuestionRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        return qw;
    }
}
